<template>
  <div>
    <el-dialog
      :visible.sync="isShow"
      :title="{ add: '新增权限', edit: '编辑权限' }[mode]"
      @close="closeEvent"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="企业可见" prop="enVisible">
          <el-switch
            v-model="form.enVisible"
            active-text="可见"
            inactive-text="不可见"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission'
export default {
  data() {
    return {
      isShow: false,
      mode: 'add', // 场景变量
      form: {
        name: '', // 	string	非必须
        code: '', // 	string	非必须:'',
        description: '', // 	string	非必须
        enVisible: '0', // 	string	非必须 '0' 和 '1'
        type: 1, // 	number	非必须  1代表页面 2代表按钮
        pid: '0' // 	string	非必须
      },
      rules: {
        name: [{ required: true, message: '必填项', trigger: 'change' }],
        code: [{ required: true, message: '必填项', trigger: 'change' }],
        description: [{ required: true, message: '必填项', trigger: 'change' }],
        enVisible: [{ required: true, message: '必填项', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 关闭弹框
    closeEvent() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: '0'
      }
    },
    // 表单提交按钮
    submit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.mode === 'add') {
            await sysPermissionPost(this.form)
            // 提示信息
            this.$message.success('数据新增成功')
          } else if (this.mode === 'edit') {
            await sysPermissionPut(this.form)
            // 提示信息
            this.$message.success('数据修改成功')
          }
          this.closeEvent()
          // 渲染父级列表数据
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
